<?xml version="1.0" encoding="UTF-8" ?>

<!--
    Document   : atom-script.xsl
    Created on : March 6, 2007, 1:24 PM
    Author     : alex
    Description:
        Purpose of transformation follows.
-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
                xmlns:s="http://www.atomojo.org/Vocabulary/AtomScript/2008/1/0"
                xmlns:oxsl="http://www.atomojo.org/XSL"
                xmlns:app="http://www.w3.org/2007/app">
<xsl:namespace-alias stylesheet-prefix="oxsl" result-prefix="xsl"/>
<xsl:output method="xml"/>

<xsl:template match="/">
   <xsl:choose>
      <xsl:when test="xsl:transform|xsl:stylesheet">
         <xsl:apply-templates/>
      </xsl:when>
      <xsl:otherwise>
         <oxsl:stylesheet xmlns:atom="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns:p='http://www.w3.org/2007/03/xproc' xmlns:s='http://www.atomojo.org/Vocabulary/AtomScript/2008/1/0' xmlns:c='http://www.w3.org/2007/03/xproc-step' xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" exclude-result-prefixes="app atom p s c">
            <oxsl:output method="xml" omit-xml-declaration="yes"/>
            <xsl:apply-templates/>
         </oxsl:stylesheet>
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>

<xsl:template match="/s:template" priority="2">
   <xsl:for-each select="s:parameter">
      <oxsl:param name="{@name}"/>
   </xsl:for-each>
   <xsl:apply-templates/>
</xsl:template>

<xsl:template match="s:parameter"/>

<xsl:template match="s:value">
   <oxsl:value-of select="{@select}"/>
</xsl:template>

<xsl:template match="/s:template/s:do">
   <xsl:text>&#xa;</xsl:text>
   <oxsl:template match="atom:feed">
      <xsl:apply-templates select="node()"/>
   </oxsl:template>
   <xsl:text>&#xa;</xsl:text>
</xsl:template>

<xsl:template match="/s:template/s:for-feed|/s:for-feed|/xsl:transform/s:for-feed|/xsl:stylesheet/s:for-feed" priority="2">
   <xsl:text>&#xa;</xsl:text>
   <oxsl:template>
      <xsl:attribute name="match">
         <xsl:text>atom:feed</xsl:text>
         <xsl:apply-templates select="*" mode="predicates"/>
      </xsl:attribute>
      <xsl:if test="@priority">
         <xsl:attribute name="priority"><xsl:value-of select="@priority"/></xsl:attribute>
      </xsl:if>
      <xsl:apply-templates select="s:do/node()"/>
   </oxsl:template>
   <xsl:text>&#xa;</xsl:text>
</xsl:template>

<xsl:template mode="predicates" match="s:do"/>

<xsl:template mode="predicates" match="s:with-content">
   <xsl:choose>
      <xsl:when test="@type">
         <xsl:text>[atom:content[starts-with(@type,'</xsl:text>
         <xsl:value-of select="@type"/>
         <xsl:text>')]]</xsl:text>
      </xsl:when>
      <xsl:otherwise>
         <xsl:text>[atom:content]</xsl:text>
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>

<xsl:template mode="predicates" match="s:without-content">
   <xsl:choose>
      <xsl:when test="@type">
         <xsl:text>[not(atom:content[starts-with(@type,'</xsl:text>
         <xsl:value-of select="@type"/>
         <xsl:text>')])]</xsl:text>
      </xsl:when>
      <xsl:otherwise>
         <xsl:text>[not(atom:content)]</xsl:text>
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>

<xsl:template mode="predicates" match="s:has">
   <xsl:call-template name="has-predicate"/>
</xsl:template>

<xsl:template mode="predicates" match="s:has-not">
   <xsl:call-template name="has-not-predicate"/>
</xsl:template>

<xsl:template mode="predicates" match="s:has-entry">
   <xsl:text>[atom:entry</xsl:text>
   <xsl:apply-templates select="*" mode="predicates"/>
   <xsl:text>]</xsl:text>
</xsl:template>

<xsl:template mode="predicates" match="*"/>

<xsl:template mode="test" match="s:has-entry">
   <xsl:text>/atom:feed/atom:entry</xsl:text>
   <xsl:apply-templates select="*" mode="predicates"/>
</xsl:template>

<xsl:template mode="test" match="s:has[not(preceding-sibling::s:has)]">
   <xsl:text>/atom:feed</xsl:text>
   <xsl:call-template name="has-predicate"/>
</xsl:template>

<xsl:template mode="test" match="s:has[preceding-sibling::s:has]">
   <xsl:call-template name="has-predicate"/>
</xsl:template>

<xsl:template mode="test" match="s:has-not[not(preceding-sibling::s:has)]">
   <xsl:text>/atom:feed</xsl:text>
   <xsl:call-template name="has-predicate"/>
</xsl:template>

<xsl:template mode="test" match="s:has-not[preceding-sibling::s:has]">
   <xsl:call-template name="has-predicate"/>
</xsl:template>

<xsl:template name="has-predicate">
   <xsl:choose>
      <xsl:when test="@term and @value">
         <xsl:text>[atom:category[concat(@scheme,@term)='</xsl:text>
         <xsl:value-of select="@term"/>
         <xsl:text>' and .='</xsl:text>
         <xsl:value-of select="@value"/>
         <xsl:text>']]</xsl:text>
      </xsl:when>
      <xsl:when test="@term">
         <xsl:text>[atom:category[concat(@scheme,@term)='</xsl:text>
         <xsl:value-of select="@term"/>
         <xsl:text>']]</xsl:text>
      </xsl:when>
   </xsl:choose>
</xsl:template>

<xsl:template name="has-not-predicate">
   <xsl:choose>
      <xsl:when test="@term and @value">
         <xsl:text>[not(atom:category[concat(@scheme,@term)='</xsl:text>
         <xsl:value-of select="@term"/>
         <xsl:text>' and .='</xsl:text>
         <xsl:value-of select="@value"/>
         <xsl:text>'])]</xsl:text>
      </xsl:when>
      <xsl:when test="@term">
         <xsl:text>[not(atom:category[concat(@scheme,@term)='</xsl:text>
         <xsl:value-of select="@term"/>
         <xsl:text>'])]</xsl:text>
      </xsl:when>
   </xsl:choose>
</xsl:template>

<xsl:template mode="test" match="*"/>

<xsl:template name="make-test">
   <xsl:if test="s:has|s:has-not">
      <xsl:for-each select="s:has|s:has-not">
         <xsl:if test="position()!=1">
            <xsl:text> and </xsl:text>
         </xsl:if>
         <xsl:text>self::*</xsl:text>
         <xsl:choose>
            <xsl:when test="self::s:has">
               <xsl:call-template name="has-predicate"/>
            </xsl:when>
            <xsl:otherwise>
               <xsl:call-template name="has-not-predicate"/>
            </xsl:otherwise>
         </xsl:choose>
      </xsl:for-each>
      <xsl:if test="s:has-entry">
         <xsl:text> and </xsl:text>
      </xsl:if>
   </xsl:if>
   <xsl:for-each select="s:has-entry">
      <xsl:if test="position()!=1">
         <xsl:text> and </xsl:text>
      </xsl:if>
      <xsl:apply-templates select="." mode="test"/>
   </xsl:for-each>
   <xsl:for-each select="s:with-content">
      <xsl:if test="position()!=1">
         <xsl:text> and </xsl:text>
      </xsl:if>
      <xsl:apply-templates select="." mode="test"/>
   </xsl:for-each>
</xsl:template>

<xsl:template match="s:for-each-entry//s:if" priority="2">
   <xsl:choose>
      <xsl:when test="@test">
         <oxsl:if test="{@test}">
            <xsl:apply-templates select="s:do/node()"/>
         </oxsl:if>
      </xsl:when>
      <xsl:otherwise>
         <oxsl:if>
            <xsl:attribute name="test">
               <xsl:text>boolean(self::*</xsl:text>
               <xsl:apply-templates select="*" mode="predicates"/>
               <xsl:text>)</xsl:text>
            </xsl:attribute>
            <xsl:apply-templates select="s:do/node()"/>
         </oxsl:if>
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>

<xsl:template match="s:if">
   <xsl:choose>
      <xsl:when test="@test">
         <oxsl:if test="{@test}">
            <xsl:apply-templates select="s:do/node()"/>
         </oxsl:if>
      </xsl:when>
      <xsl:otherwise>
         <oxsl:if>
            <xsl:attribute name="test">
               <xsl:text>/atom:feed</xsl:text>
               <xsl:apply-templates select="*" mode="predicates"/>
            </xsl:attribute>
            <xsl:apply-templates select="s:do/node()"/>
         </oxsl:if>
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>

<xsl:template match="s:choose">
   <oxsl:choose>
      <xsl:for-each select="s:when">
         <xsl:choose>
            <xsl:when test="@test">
               <oxsl:when test="{@test}">
                  <xsl:apply-templates select="s:do/node()"/>
               </oxsl:when>
            </xsl:when>
            <xsl:otherwise>
               <oxsl:when>
                  <xsl:attribute name="test">
                     <xsl:text>/atom:feed</xsl:text>
                     <xsl:apply-templates select="*" mode="predicates"/>
                  </xsl:attribute>
                  <xsl:apply-templates select="s:do/node()"/>
               </oxsl:when>
            </xsl:otherwise>
         </xsl:choose>
      </xsl:for-each>
      <xsl:for-each select="s:otherwise">
      <oxsl:otherwise>
            <xsl:apply-templates select="s:do/node()"/>
         </oxsl:otherwise>
      </xsl:for-each>
   </oxsl:choose>
</xsl:template>

<xsl:template match="s:for-each-entry//s:choose" priority="2">
   <oxsl:choose>
      <xsl:for-each select="s:when">
         <xsl:choose>
            <xsl:when test="@test">
               <oxsl:when test="{@test}">
                  <xsl:apply-templates select="s:do/node()"/>
               </oxsl:when>
            </xsl:when>
            <xsl:otherwise>
               <oxsl:when>
                  <xsl:attribute name="test">
                     <xsl:text>boolean(self::*</xsl:text>
                     <xsl:apply-templates select="*" mode="predicates"/>
                     <xsl:text>)</xsl:text>
                  </xsl:attribute>
                  <xsl:apply-templates select="s:do/node()"/>
               </oxsl:when>
            </xsl:otherwise>
         </xsl:choose>
      </xsl:for-each>
      <xsl:for-each select="s:otherwise">
      <oxsl:otherwise>
            <xsl:apply-templates select="s:do/node()"/>
         </oxsl:otherwise>
      </xsl:for-each>
   </oxsl:choose>
</xsl:template>

<xsl:template match="s:for-feed" priority="1.2">
   <oxsl:for-each>
      <xsl:attribute name="select">
         <xsl:text>/atom:feed</xsl:text>
         <xsl:apply-templates select="*" mode="predicates"/>
      </xsl:attribute>
      <xsl:apply-templates select="s:do/node()"/>
   </oxsl:for-each>
</xsl:template>

<xsl:template match="s:for-each-entry">
   <oxsl:for-each>
      <xsl:attribute name="select">
         <xsl:text>/atom:feed/atom:entry</xsl:text>
         <xsl:apply-templates select="*" mode="predicates"/>
      </xsl:attribute>
      <xsl:apply-templates select="s:sort|s:sort-term"/>
      <xsl:apply-templates select="s:do/node()"/>
   </oxsl:for-each>
</xsl:template>

<xsl:template match="s:sort-term">
   <oxsl:sort select="atom:category[concat(@scheme,@term)='{@term}']">
      <xsl:copy-of select="@lang|@data-type|@order|@case-order"/>
   </oxsl:sort>
</xsl:template>

<xsl:template match="s:sort">
   <oxsl:sort>
      <xsl:copy-of select="@*"/>
   </oxsl:sort>
</xsl:template>

<xsl:template match="s:has"/>
<xsl:template match="s:with-content"/>
<xsl:template match="s:has-not"/>
<xsl:template match="s:has-entry"/>
<xsl:template match="s:for-each-entry/s:do">
   <xsl:apply-templates/>
</xsl:template>

<xsl:template match="s:text">
   <xsl:choose>
      <xsl:when test="@ref='content'">
         <oxsl:copy-of select="atom:content/node()"/>
      </xsl:when>
      <xsl:when test="@ref='title'">
         <oxsl:copy-of select="atom:title/node()"/>
      </xsl:when>
      <xsl:when test="@ref='summary'">
         <oxsl:copy-of select="atom:summary/node()"/>
      </xsl:when>
      <xsl:when test="@ref='published'">
         <oxsl:copy-of select="atom:published/node()"/>
      </xsl:when>
      <xsl:when test="@ref='updated'">
         <oxsl:copy-of select="atom:published/node()"/>
      </xsl:when>
      <xsl:when test="@ref='id'">
         <oxsl:copy-of select="atom:id/node()"/>
      </xsl:when>
      <xsl:when test="@ref='edited'">
         <oxsl:copy-of select="app:edited/node()"/>
      </xsl:when>
      <xsl:when test="@ref='category'">
         <oxsl:value-of>
            <xsl:attribute name="select">
               <xsl:text>atom:category[concat(@scheme,@term)='</xsl:text>
               <xsl:value-of select="@term"/>
               <xsl:text>']</xsl:text>
            </xsl:attribute>
         </oxsl:value-of>
      </xsl:when>
      <xsl:otherwise>
         <oxsl:text><xsl:value-of select="."/></oxsl:text>
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>

<xsl:template match="s:term">
   <oxsl:value-of select="atom:category[concat(@scheme,@term)='{@ref}']"/>
</xsl:template>

<xsl:template match="s:for-each-link">
   <xsl:choose>
      <xsl:when test="@rel">
         <oxsl:for-each select="atom:link[@rel='{@rel}']">
            <xsl:apply-templates select="node()"/>
         </oxsl:for-each>
      </xsl:when>
      <xsl:otherwise>
         <oxsl:for-each select="atom:link">
            <xsl:apply-templates select="node()"/>
         </oxsl:for-each>
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>

<xsl:template match="s:for-each-term">
   <xsl:choose>
      <xsl:when test="string-length(@scheme)">
         <oxsl:for-each select="atom:category[@scheme='{@scheme}']">
            <xsl:apply-templates select="node()"/>
         </oxsl:for-each>
      </xsl:when>
      <xsl:when test="string-length(@term)">
         <oxsl:for-each select="atom:category[concat(@scheme,@term)='{@term}']">
            <xsl:apply-templates select="node()"/>
         </oxsl:for-each>
      </xsl:when>
      <xsl:when test="@scheme=''">
         <oxsl:for-each select="atom:category[not(@scheme)]">
            <xsl:apply-templates select="node()"/>
         </oxsl:for-each>
      </xsl:when>
      <xsl:otherwise>
         <oxsl:for-each select="atom:category">
            <xsl:apply-templates select="node()"/>
         </oxsl:for-each>
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>

<xsl:template match="s:feed-ref">
   <xsl:choose>
      <xsl:when test="@rel">
         <oxsl:for-each select="atom:link[@rel='{@rel}']">
            <s:feed-include href="{{@href}}"/>
         </oxsl:for-each>
      </xsl:when>
      <xsl:otherwise>
          <oxsl:variable name="href"><ref href="{@href}"/></oxsl:variable>
          <s:feed-include href="{{resolve-uri($href/ref/@href,'{base-uri(.)}')}}"/>
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>

<xsl:template match="s:for-each">
   <oxsl:for-each select="{@select}">
      <xsl:copy>
         <xsl:apply-templates select="@*"/>
      </xsl:copy>
      <xsl:apply-templates/>
   </oxsl:for-each>
</xsl:template>

<xsl:template match="s:copy">
   <oxsl:copy>
      <xsl:apply-templates/>
   </oxsl:copy>
</xsl:template>

<xsl:template match="s:copy-of">
   <oxsl:copy-of select="{@select}"/>
</xsl:template>


<xsl:template match="/xsl:transform|/xsl:stylesheet" priority="2">
   <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
   </xsl:copy>
</xsl:template>

<xsl:template match="/*">
   <xsl:text>&#xa;</xsl:text>
   <oxsl:template match="/">
   <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
   </xsl:copy>
   </oxsl:template>
   <xsl:text>&#xa;</xsl:text>
</xsl:template>

<xsl:template match="*|@*">
   <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
   </xsl:copy>
</xsl:template>

</xsl:stylesheet>
